package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.ShoppingCart;
import cn.wolfcode.trip.base.mapper.ShoppingCartMapper;
import cn.wolfcode.trip.base.service.IShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/** tiger
 * 购物车
 */
@Service
public class ShoppingCartServiceImpl implements IShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /** tiger
     * 通过用户id 查询购物车中信息
     * @param userId
     * @return
     */
    public List<ShoppingCart> getShoppingCartByUserId(Long userId) {
        List<ShoppingCart> list=shoppingCartMapper.ListByUserId(userId);
        return list;
    }

    /** tiger
     * 获取购物车信息
     * @param ids
     * @return
     */
    public List<ShoppingCart> getShoppingCartById(int[] ids) {
        List<ShoppingCart> list=shoppingCartMapper.getShoppingCartById(ids);
        return list;
    }

    /**
     * 新增或更新购物车
     * @param shoppingCart
     */
    public void saveOrUpdate(ShoppingCart shoppingCart) {

        if (shoppingCart.getId()!= null) {
            shoppingCartMapper.updateByPrimaryKey(shoppingCart);
        }else{
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /** tiger
     * 根据id删除购物车信息
     * @param ids
     */
    public void deleteShoppingCartById(int[] ids) {
        shoppingCartMapper.deleteShoppingCartById(ids);
    }



    /**
     * 商品添加到购物车 返回id
     * @param shoppingCart
     * @return
     */
    @Override
    public ShoppingCart saveAndReutenId(ShoppingCart shoppingCart) {
        shoppingCartMapper.insert(shoppingCart);
        return shoppingCart;
    }
}
